﻿
// Copyright (c) 2010-2021 niXman (github dot nixman at pm dot me). All
// rights reserved.
//
// This file is part of YAS(https://github.com/niXman/yas) project.
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#ifndef __yas__serialize_hpp
#define __yas__serialize_hpp

#include <yas/binary_oarchive.hpp>
#include <yas/binary_iarchive.hpp>
#include <yas/text_oarchive.hpp>
#include <yas/text_iarchive.hpp>
#include <yas/json_oarchive.hpp>
#include <yas/json_iarchive.hpp>
#include <yas/mem_streams.hpp>
#include <yas/file_streams.hpp>
#include <yas/std_streams.hpp>
#include <yas/count_streams.hpp>

namespace yas
{
    // F: 数据序列化的方式，包括memory，file，json等

    /***************************************************************************/
    // mem + binary
    // 要使用该函数，需要满足：
    // 1. (F& yas::mem) && (F& yas::binary) 为真
    // 2. 返回值为yas::shared_buffer
    // 3. 符合要求的输入参数类型
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::binary), yas::shared_buffer>::type save(
        Types &&...args)
    {
        yas::mem_ostream os;
        yas::binary_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);

        return os.get_shared_buffer();
    }

    // 1. 满足(F& yas::mem) && (F& yas::binary) 为真
    // in/out：yas::mem_ostream &os
    // in：Types &&...args
    // out：void
    // 2. 符合要求的输入参数类型
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::binary)>::type save(yas::mem_ostream &os, Types &&...args)
    {
        yas::binary_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // in/out：yas::mem_ostream &&os
    // in：Types &&...args
    // out：void
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::binary)>::type save(yas::mem_ostream &&os, Types &&...args)
    {
        yas::binary_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // in/out：yas::vector_ostream<Byte> &os
    // in：Types &&...args
    // out：void
    template <std::size_t F, typename Byte, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::binary)>::type save(yas::vector_ostream<Byte> &os, Types &&...args)
    {
        yas::binary_oarchive<yas::vector_ostream<Byte>, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // in/out：yas::vector_ostream<Byte> &os
    // in：Types &&...args
    // out：void
    template <std::size_t F, typename Byte, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::binary)>::type save(yas::vector_ostream<Byte> &&os, Types &&...args)
    {
        yas::binary_oarchive<yas::vector_ostream<Byte>, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // 将数据序列化为内存中的文本格式
    // mem + text

    // in: Types &&...args
    // out: yas::shared_buffer
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::text), yas::shared_buffer>::type save(Types &&...args)
    {
        yas::mem_ostream os;
        yas::text_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);

        return os.get_shared_buffer();
    }

    // mem + text
    // in/out: yas::mem_ostream &os
    // in: Types &&...args
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::text)>::type save(yas::mem_ostream &os, Types &&...args)
    {
        yas::text_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // mem + text
    // in/out: yas::mem_ostream &os
    // in: Types &&...args
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::text)>::type save(yas::mem_ostream &&os, Types &&...args)
    {
        yas::text_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // mem + text
    // in/out: yas::vector_ostream<Byte> &os
    // in: Types &&...args
    template <std::size_t F, typename Byte, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::text)>::type save(yas::vector_ostream<Byte> &os, Types &&...args)
    {
        yas::text_oarchive<yas::vector_ostream<Byte>, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // mem + text
    // in/out: yas::vector_ostream<Byte> &&os
    // in: Types &&...args
    template <std::size_t F, typename Byte, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::text)>::type save(yas::vector_ostream<Byte> &&os, Types &&...args)
    {
        yas::text_oarchive<yas::vector_ostream<Byte>, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // mem + json
    
    /// @brief 将args序列化到yas::shared_buffer中
    /// @tparam ...Types 
    /// @tparam F 
    /// @param ...args 
    /// @return yas::shared_buffer
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::json), yas::shared_buffer>::type save(Types &&...args)
    {
        yas::mem_ostream os;
        yas::json_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);

        return os.get_shared_buffer();
    }

    /// @brief 将args序列化到yas::mem_ostream中
    /// @tparam ...Types 
    /// @tparam F 
    /// @param os 
    /// @param ...args 
    /// @return void
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::json)>::type save(yas::mem_ostream &os, Types &&...args)
    {
        yas::json_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }


    /// @brief 将args序列化到yas::mem_ostream中
    /// @tparam ...Types 
    /// @tparam F 
    /// @param os 
    /// @param ...args 
    /// @return void
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::json)>::type save(yas::mem_ostream &&os, Types &&...args)
    {
        yas::json_oarchive<yas::mem_ostream, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /// @brief 将args序列化到yas::vector_ostream<Byte>中
    /// @tparam Byte 
    /// @tparam ...Types 
    /// @tparam F 
    /// @param os 
    /// @param ...args 
    /// @return void
    template <std::size_t F, typename Byte, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::json)>::type save(yas::vector_ostream<Byte> &os, Types &&...args)
    {
        yas::json_oarchive<yas::vector_ostream<Byte>, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /// @brief 将args序列化到yas::vector_ostream<Byte>中
    /// @tparam Byte 
    /// @tparam ...Types 
    /// @tparam F 
    /// @param os 
    /// @param ...args 
    /// @return void
    template <std::size_t F, typename Byte, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::json)>::type save(yas::vector_ostream<Byte> &&os, Types &&...args)
    {
        yas::json_oarchive<yas::vector_ostream<Byte>, (F & (~yas::mem))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // file name

    /// @brief 将args序列化到fname二进制文件中
    /// @tparam ...Types 
    /// @tparam F 
    /// @param fname 
    /// @param ...args 
    /// @return void
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::binary)>::type save(const char *fname, Types &&...args)
    {
        yas::file_ostream os(fname);
        // 定义二进制文档对象
        yas::binary_oarchive<yas::file_ostream, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /// @brief 将args序列化到fname文本文件中
    /// @tparam ...Types 
    /// @tparam F 
    /// @param fname 
    /// @param ...args 
    /// @return 
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::text)>::type save(const char *fname, Types &&...args)
    {
        yas::file_ostream os(fname);
        // 定义文本文档对象oa
        yas::text_oarchive<yas::file_ostream, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /// @brief 将args序列化到文件名为fname的json文件中
    /// @tparam ...Types 
    /// @tparam F 
    /// @param fname 
    /// @param ...args 
    /// @return 
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::json)>::type save(const char *fname, Types &&...args)
    {
        yas::file_ostream os(fname);
        // 定义json文档对象
        yas::json_oarchive<yas::file_ostream, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // yas::file_ostream
    // 将args序列化到文件名为二进制文件输出流os中
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::binary)>::type save(yas::file_ostream &os, Types &&...args)
    {
        yas::binary_oarchive<yas::file_ostream, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // 将args序列化到文件名为文本文件输出流os中
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::text)>::type save(yas::file_ostream &os, Types &&...args)
    {
        yas::text_oarchive<yas::file_ostream, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    // 将args序列化到文件名为json文件输出流os中
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::json)>::type save(yas::file_ostream &os, Types &&...args)
    {
        yas::json_oarchive<yas::file_ostream, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // yas::std_ostream_adapter

    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::binary)>::type
    save(yas::std_ostream_adapter &os, Types &&...args)
    {
        yas::binary_oarchive<yas::std_ostream_adapter, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::text)>::type
    save(yas::std_ostream_adapter &os, Types &&...args)
    {
        yas::text_oarchive<yas::std_ostream_adapter, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::json)>::type
    save(yas::std_ostream_adapter &os, Types &&...args)
    {
        yas::json_oarchive<yas::std_ostream_adapter, (F & (~yas::file))> oa(os);
        oa(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // byte counter
    template <std::size_t F, typename... Types>
    typename std::enable_if<((F & yas::binary) > 0), std::size_t>::type
    saved_size(Types &&...args)
    {
        yas::count_ostream os;
        yas::binary_oarchive<yas::count_ostream, F> oa(os);
        oa(std::forward<Types>(args)...);
        return os.total_size;
    }

    template <std::size_t F, typename... Types>
    typename std::enable_if<((F & yas::text) > 0), std::size_t>::type
    saved_size(Types &&...args)
    {
        yas::count_ostream os;
        yas::text_oarchive<yas::count_ostream, F> oa(os);
        oa(std::forward<Types>(args)...);
        return os.total_size;
    }

    template <std::size_t F, typename... Types>
    typename std::enable_if<((F & yas::json) > 0), std::size_t>::type
    saved_size(Types &&...args)
    {
        yas::count_ostream os;
        yas::json_oarchive<yas::count_ostream, F> oa(os);
        oa(std::forward<Types>(args)...);
        return os.total_size;
    }

    /***************************************************************************/
    // mem
    // 从二进制内存中加载数据到args
    template <std::size_t F, typename Buf, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::binary)>::type
    load(const Buf &buf, Types &&...args)
    {
        yas::mem_istream is(buf);
        yas::binary_iarchive<yas::mem_istream, (F & (~yas::mem))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    // 从文本内存中加载数据到args
    template <std::size_t F, typename Buf, typename... Types>
    typename std::enable_if<(F & yas::mem) && (F & yas::text)>::type
    load(const Buf &buf, Types &&...args)
    {
        yas::mem_istream is(buf);
        yas::text_iarchive<yas::mem_istream, (F & (~yas::mem))> ia(is);
        ia(std::forward<Types>(args)...);
    }

     // 从json内存中加载数据到args 
    template <std::size_t F, typename Buf, typename... Types>
    typename std::enable_if<
        (F & yas::mem) && (F & yas::json)>::type
    load(const Buf &buf, Types &&...args)
    {
        yas::mem_istream is(buf);
        yas::json_iarchive<yas::mem_istream, (F & (~yas::mem))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // file name
    // 从二进制文件中加载数据到args中
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::binary)>::type
    load(const char *fname, Types &&...args)
    {
        yas::file_istream is(fname);
        yas::binary_iarchive<yas::file_istream, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    // 从文本文件中加载数据到args中
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::text)>::type
    load(const char *fname, Types &&...args)
    {
        yas::file_istream is(fname);
        yas::text_iarchive<yas::file_istream, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    // 从json文件中加载数据到args中
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::json)>::type
    load(const char *fname, Types &&...args)
    {
        yas::file_istream is(fname);
        yas::json_iarchive<yas::file_istream, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // yas::file_istream
    // 从二进制文件流中加载数据
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::binary)>::type
    load(yas::file_istream &is, Types &&...args)
    {
        yas::binary_iarchive<yas::file_istream, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    // 从文本文件流中加载数据
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::text)>::type
    load(yas::file_istream &is, Types &&...args)
    {
        yas::text_iarchive<yas::file_istream, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    // 从json文件流中加载数据
    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::json)>::type
    load(yas::file_istream &is, Types &&...args)
    {
        yas::json_iarchive<yas::file_istream, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    /***************************************************************************/
    // yas::std_istream_adapter

    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::binary)>::type
    load(yas::std_istream_adapter &is, Types &&...args)
    {
        yas::binary_iarchive<yas::std_istream_adapter, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::text)>::type 
    load(yas::std_istream_adapter &is, Types &&...args)
    {
        yas::text_iarchive<yas::std_istream_adapter, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }


    template <std::size_t F, typename... Types>
    typename std::enable_if<(F & yas::file) && (F & yas::json)>::type 
    load(yas::std_istream_adapter &is, Types &&...args)
    {
        yas::json_iarchive<yas::std_istream_adapter, (F & (~yas::file))> ia(is);
        ia(std::forward<Types>(args)...);
    }

    /***************************************************************************/

} // namespace yas

#endif // __yas__serialize_hpp
